package com.jiangyg.mall.product.controller;

import com.jiangyg.mall.core.exception.BusinessException;
import com.jiangyg.mall.core.support.restful.Pagination;
import com.jiangyg.mall.core.support.restful.Result;
import com.jiangyg.mall.product.dto.BrandDTO;
import com.jiangyg.mall.product.dto.CategoryBrandDTO;
import com.jiangyg.mall.product.vo.BrandVO;
import com.jiangyg.mall.product.vo.CategoryBrandVO;
import com.jiangyg.mall.product.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 类描述：品牌控制器
 *
 * @author jiangyg
 * @version email@jiangyg.com
 * @date 2021-10-30 07:38:49
 */
@RestController
@RequestMapping("/product/brand")
public class BrandController {

    /**
     * 品牌接口
     */
    private final BrandService brandService;

    @Autowired
    public BrandController(BrandService brandService) {
        this.brandService = brandService;
    }

    /**
     * 功能描述：品牌分页查询
     *
     * @param request    请求
     * @param pagination 分页参数
     * @return 返回分页品牌
     */
    @ResponseBody
    @PostMapping("/select")
    public Result<Pagination<BrandVO>> select(HttpServletRequest request, @RequestBody Pagination<BrandVO> pagination) throws Exception {
        return Result.ok(brandService.selectPagination(pagination));
    }

    /**
     * 功能描述：保存或者修改品牌
     *
     * @param request 请求
     * @param dto     品牌
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/save")
    public Result<Object> save(HttpServletRequest request, @RequestBody @Validated BrandDTO dto) throws Exception {
        return Result.saveOrUpdate(brandService.saveOrUpdate(dto));
    }

    /**
     * 功能描述：根据ID获取品牌
     *
     * @param request 请求
     * @param brandId 品牌ID
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/info")
    public Result<BrandVO> info(HttpServletRequest request, @RequestParam(name = "brandId") Long brandId) throws Exception {
        return Result.ok(brandService.selectById(brandId));
    }

    /**
     * 功能描述：删除品牌
     *
     * @param request  请求
     * @param brandIds 批量删除的品牌ID
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/delete")
    public Result<Object> delete(HttpServletRequest request, @RequestBody List<Long> brandIds) {
        return Result.delete(brandService.delete(brandIds));
    }

    /**
     * 功能描述：品牌分类关联列表
     *
     * @param dto 参数
     * @return 返回分页品牌
     */
    @ResponseBody
    @PostMapping("/relation/list")
    public Result<List<CategoryBrandVO>> relationList(@RequestBody CategoryBrandDTO dto) throws Exception {
        if (dto.getBrandId() == null && dto.getCategoryId() == null) {
            throw BusinessException.instance("mall.product.brand.params.error");
        }
        return Result.ok(brandService.relationList(dto));
    }

    /**
     * 功能描述：保存或者修改品牌分类关联
     *
     * @param request 请求
     * @param dto     品牌
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/relation/save")
    public Result<Object> relationSave(HttpServletRequest request, @RequestBody @Validated CategoryBrandDTO dto) throws Exception {
        return Result.saveOrUpdate(brandService.relationSave(dto));
    }

    /**
     * 功能描述：删除品牌分类关联
     *
     * @param request 请求
     * @param dto     品牌
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/relation/delete")
    public Result<Object> relationDelete(HttpServletRequest request, @RequestBody @Validated CategoryBrandDTO dto) {
        return Result.delete(brandService.relationDelete(dto));
    }

}
